using System;
using System.Collections;
using System.IO;
using System.Security.Cryptography;
using System.Text;

    public class CCryptoManager
    {
        private static CCryptoManager m_CryptoManager = null;

        private static int m_iInitStatus = 1;

        private string m_strError = "";

        private string strPassword="realbidz@klmalaysia";
        private string strSalt="start@20070723";
        private string strHashName="SHA1";
        private string strIterations = "2";
        private string strKeySize = "256";
        private string strIV = "eviheeb@52298552";

        private CCryptoManager()
        {

        }

        public string ErrorMessage
        {
            get
            {
                return m_strError;
            }
        }

        public int InitStatus
        {
            get
            {
                return m_iInitStatus;
            }
        }

        public static CCryptoManager create()
        {
            if (m_CryptoManager == null)
            {
                m_CryptoManager = new CCryptoManager();

                if (m_iInitStatus == -1)
                    m_CryptoManager = null;
            }

            return m_CryptoManager;
        }

        public int decryptText(ref string strResult, string strSource)
        {
            if (m_iInitStatus == -1)
                return -1;


            strResult = "";

            bool bError = false;

            try
            {
                PasswordDeriveBytes passwordDeriveBytes = new PasswordDeriveBytes(strPassword,
                                                                                  Encoding.ASCII.GetBytes(strSalt),
                                                                                  strHashName,
                                                                                  Convert.ToInt32(strIterations));

                RijndaelManaged rijndaelManaged = new RijndaelManaged();

                rijndaelManaged.Mode = CipherMode.CBC;

                ICryptoTransform iCryptoTransform = rijndaelManaged.CreateDecryptor(passwordDeriveBytes.GetBytes(Convert.ToInt32(strKeySize) / 8),
                                                                                    Encoding.ASCII.GetBytes(strIV));

                byte[] abyteSource = Convert.FromBase64String(strSource);

                MemoryStream memoryStream = new MemoryStream(abyteSource);

                CryptoStream cryptoStream = new CryptoStream(memoryStream, iCryptoTransform, CryptoStreamMode.Read);

                byte[] abyteResult = new byte[abyteSource.Length];

                int iDecryptedByteCount = cryptoStream.Read(abyteResult, 0, abyteResult.Length);

                memoryStream.Close();

                cryptoStream.Close();

                strResult = Encoding.UTF8.GetString(abyteResult, 0, iDecryptedByteCount);
            }

            catch
            {
                bError = true;
            }

            return (bError) ? -1 : 0;
        }

        public int encryptText(ref string strResult, string strSource)
        {
            if (m_iInitStatus == -1)
                return -1;

            strResult = "";

            bool bError = false;

            try
            {
                PasswordDeriveBytes passwordDeriveBytes = new PasswordDeriveBytes(strPassword,
                                                                                  Encoding.ASCII.GetBytes(strSalt),
                                                                                  strHashName,
                                                                                  Convert.ToInt32(strIterations));

                RijndaelManaged rijndaelManaged = new RijndaelManaged();

                rijndaelManaged.Mode = CipherMode.CBC;

                ICryptoTransform iCryptoTransform = rijndaelManaged.CreateEncryptor(passwordDeriveBytes.GetBytes(Convert.ToInt32(strKeySize) / 8),
                                                                                    Encoding.ASCII.GetBytes(strIV));

                MemoryStream memoryStream = new MemoryStream();

                CryptoStream cryptoStream = new CryptoStream(memoryStream, iCryptoTransform, CryptoStreamMode.Write);

                byte[] abyteSource = Encoding.UTF8.GetBytes(strSource);

                cryptoStream.Write(abyteSource, 0, abyteSource.Length);

                cryptoStream.FlushFinalBlock();

                byte[] abyteResult = memoryStream.ToArray();

                memoryStream.Close();

                cryptoStream.Close();

                strResult = Convert.ToBase64String(abyteResult);
            }

            catch
            {
                bError = true;
            }

            return (bError) ? -1 : 0;
        }

    }
